using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Widgetsphere.Generator.Common.GeneratorFramework;

namespace Widgetsphere.Generator.Common.Forms
{
  public class ListViewComparer : System.Collections.IComparer
  {
    #region Class Members

    private Array _typeList = null;
    private ListView _listView = null;
    protected int _column = -1;

    #endregion

    #region Constructor

    public ListViewComparer(ListView listView, Array typeList)
    {
      _listView = listView;
      _listView.ColumnClick += new ColumnClickEventHandler(ErrorControl_ColumnClick);
      _typeList = typeList;
    }

    #endregion

    #region Property Implementations

    public int Column
    {
      get { return _column; }
      set { _column = value; }
    }

    protected Array TypeList
    {
      get { return _typeList; }
    }

    protected ListView ListView
    {
      get { return _listView; }
    }

    #endregion

    #region IComparer Members

    public int Compare(object x, object y)
    {
      if (this.Column == -1)
        return 0;

      ListViewItem item1 = (ListViewItem)x;
      ListViewItem item2 = (ListViewItem)y;

      System.Type type = typeof(string);
      if ((0 < this.Column) && (this.Column < this.TypeList.Length))
        type = (System.Type)this.TypeList.GetValue(this.Column);

      if (type == typeof(int))
      {
        try
        {
          int int1 = int.Parse(item1.SubItems[this.Column].Text);
          int int2 = int.Parse(item2.SubItems[this.Column].Text);
          if (int1 == int2)
            return 0;
          else if (this.ListView.Sorting == SortOrder.Ascending)
            return int1 < int2 ? -1 : 1;
          else
            return int1 > int2 ? -1 : 1;
        }
        catch { }
      }
      //else if (type == typeof(MessageTypeConstants))
      //{
      //}
      else //All other cases are strings
      {
        string text1 = item1.SubItems[this.Column].Text;
        string text2 = item2.SubItems[this.Column].Text;
        if (text1 == text2)
          return 0;
        else if (this.ListView.Sorting == SortOrder.Ascending)
          return string.Compare(text1, text2, true);
        else
          return -string.Compare(text1, text2, true);
      }

      return 0;

    }

    #endregion

    #region Event Handlers
    
    private void ErrorControl_ColumnClick(object sender, ColumnClickEventArgs e)
    {
      if (e.Column == this.Column)
      {
        if (this.ListView.Sorting == SortOrder.Descending)
          this.ListView.Sorting = SortOrder.Ascending;
        else
          this.ListView.Sorting = SortOrder.Descending;        
      }
      else
      {
        this.Column = e.Column;
        this.ListView.Sorting = SortOrder.Ascending;
      }
      this.ListView.Sort();
    }

    #endregion

  }
}